//
//  XQCourseSecondTableViewCell.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/11/29.
//  Copyright © 2016年 校企桥. All rights reserved.
//

import UIKit


class CourseSecondCollectViewCell: UICollectionViewCell {
    var icon = UIImageView()
    var label = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        InitUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var courseMode:XQHomeCourseMode?
    {
        didSet{
            let url = URL.init(string: (courseMode?.course_photo)!)
            icon.kf.setImage(with: url)
            label.text = courseMode?.course_name
        
        }
    
    }
    
    
    func InitUI(){
        icon = UIImageView.init(frame: CGRect.zero)
        self.contentView .addSubview(icon)
        
        label = UILabel.init(frame: CGRect.zero)
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.lightGray
        label.textAlignment = .center
        label.numberOfLines = 2
        self.contentView .addSubview(label)
        
        icon.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView.snp.left).offset(5)
            make.right.equalTo(self.contentView.snp.right).offset(-5)
            make.top.equalTo(self.contentView.snp.top).offset(5)
            make.bottom.equalTo(self.contentView.snp.bottom).offset(-30)
        }
        
        label.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView.snp.centerX)
            make.centerY.equalTo(self.contentView.snp.bottom).offset(-15)
            make.width.equalTo(self.frame.width-10)
        }
        
        
    }
    
}


class XQCourseSecondTableViewCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {

    public var courseself = XQCourseViewController()
    
    var collectionView: UICollectionView!
    var dataArray = [XQHomeCourseMode]()
    
    private  let CourseSecondCollectViewCellID = "CourseSecondCollectViewCellID"

    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        InitUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func InitUI() {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: 120, height: 100)
        
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 100), collectionViewLayout: layout)
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.showsVerticalScrollIndicator = false
        self.collectionView.showsHorizontalScrollIndicator = false
        self.collectionView.bounces = false
        self.collectionView.backgroundColor = UIColor.white
        self.contentView .addSubview( self.collectionView)
        
        self.collectionView.register(CourseSecondCollectViewCell.self, forCellWithReuseIdentifier: CourseSecondCollectViewCellID)
        
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CourseSecondCollectViewCellID, for: indexPath) as!CourseSecondCollectViewCell
        cell.courseMode = dataArray[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let  detailVc =  XQCourseDetailViewController()
        let courseMode = dataArray[indexPath.row]
        detailVc.cid = (courseMode.id)!
        detailVc.type = (courseMode.type)!
        detailVc.hidesBottomBarWhenPushed = true
        courseself.navigationController?.pushViewController(detailVc, animated: true)

    }

}
